<?php

namespace App\Api\Service;

use App\Api\Repositories\Log\LogUserFundRepositories;
use App\Api\Repositories\Log\LogSysMessageRepositories;

class UserLogService extends BaseService
{
    protected $log_user_fund_repositories;
    protected $log_sys_message_repositories;

    public function __construct()
    {
        $this->log_user_fund_repositories = new LogUserFundRepositories();
        $this->log_sys_message_repositories = new LogSysMessageRepositories();
    }

    public function fund_log($uid, $page, $limit = 10)
    {
        $data = $this->log_user_fund_repositories->get_list($uid, $page, $limit)->toArray();
        $data['all_number'] = $this->log_user_fund_repositories->get_count($uid);
        return $data;
    }

    public function sys_message_log($uid, $page, $limit = 10)
    {
        $list_read = config('admin.sys_message.list_read');
        $data = $this->log_sys_message_repositories->get_list($uid, $page, $limit);
        foreach ($data as &$value) {
            $value->is_read = $this->log_sys_message_repositories->get_read_status($uid, $value->id);
            if ($list_read) {
                $this->log_sys_message_repositories->set_read_status($uid, $value->id);
            }
        }
        return $data;
    }

    public function sys_message_detail($uid, $id)
    {
        $data = $this->log_sys_message_repositories->get_one($uid, $id);
        $this->log_sys_message_repositories->set_read_status($uid, $data->id);  # 设置为已读（无论设置如何，这里必须设置已读）
        return $data;
    }

    /**
     * 获取浏览记录
     *
     * @param [type] $uid
     * @return void
     */
    public function get_browse_log($uid)
    {
        $log_r = new \App\Api\Repositories\Log\LogUserBrowseRepositories();
        $log_list = $log_r->use_uid_get_log($uid);
        $data = [];
        $i = 0;
        foreach ($log_list as $v) {
            if (substr($v->result_id, 0, 1) == 'i') {
                $data[] = $this->make_interior_result_use_list_show($uid, $v->detail);
            } else {
                $data[] = $this->make_exterior_result_use_list_show($uid, $v->detail);
            }
            $data[$i]['created_at'] = date("Y-m-d H:i:s", strtotime($v->updated_at));
            $i++;
        }
        return $data;
    }

    /**
     * 会员收藏记录
     *
     * @param int $uid
     * @param int $page
     * @param int $limit
     * @return void
     */
    public function get_collect_log($uid, $page, $limit)
    {
        $log_r = new \App\Api\Repositories\Log\LogUserCollectRepositories();
        $log_list = $log_r->use_uid_get_log($uid, $page, $limit);
        $data = ['all_number' => $log_r->get_user_collect_count($uid), 'data' => []];
        foreach ($log_list as $v) {
            if (substr($v->result_id, 0, 1) == 'i') {
                $temp = $this->make_interior_result_use_list_show($uid, $v->detail);
            } else {
                $temp = $this->make_exterior_result_use_list_show($uid, $v->detail);
            }
            $temp['collect_number'] = $log_r->get_user_be_collect_count(substr($v->result_id, 1, 100));
            $data['data'][] = $temp;
        }
        return $data;
    }
}
